/*  This file is part of LitSearch.

    LitSearch is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    LitSearch is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with LitSearch; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

package de.fhbrs.litsearch;

import java.util.regex.Pattern;

import com.softtech.jdbc.SQLResults;

import de.fhbrs.ConfigReader.ConfigReader;
import de.fhbrs.ConfigReader.KeyNotFoundException;

/**
 * Lists all pending media that have to be activated
 *
 * @version 	1.0 6 Jan 2006
 * @author 		Marco Werner, Jens Uwe Zink
 * 
  */
public class MediaPendingBean {
	private LanguageBean languageObj;

	/** 
     * Empty constructor
     */
	public MediaPendingBean() {
	}
	
	/** 
     * Get all pending media in HTML table
     * @return String with HTML table
     */
	public String getMediaPending() {
		
		String mediaPendingInHTMLTable = "";
		String color = "";
		String mediumType = "";
		JDBCBean dbCon = new JDBCBean();
		SQLResults res = dbCon.getResultSet("SELECT * FROM "+ConfigReader.getInstance().readKey("litsearch.global.db.tbl.tempLiteratur")+" WHERE released=\"0\" and deleted=\"0\"");
		
		if (res.getRowCount() == 0) mediaPendingInHTMLTable = this.languageObj.getTextFor("admin.noPendingMedia");
		else {
			mediaPendingInHTMLTable += "<table class=\"users\">";
			mediaPendingInHTMLTable += "<tr><th>ID</th><th>" + this.languageObj.getTextFor("search.Type") + "</th><th>" + this.languageObj.getTextFor("search.Author") + "</th><th>" + this.languageObj.getTextFor("search.Title") + "</th><th>" + this.languageObj.getTextFor("search.Year") + "</th><th></th></tr>";
			for (int i=0; i<res.getRowCount(); i++) {
				SQLResults files = dbCon.getResultSet("SELECT * FROM "+ConfigReader.getInstance().readKey("litsearch.global.db.tbl.datastreams")+" WHERE temp_id = "+res.getLong(i, "temp_id"));
				if (i % 2 == 1) color = "class=\"darkgrey\"";
				else color = "class=\"lightgrey\"";
				if (res.getString(i, "litart").equals("book")) mediumType = "Buch";
				if (res.getString(i, "litart").equals("greylit")) mediumType = "Graue Literatur";
				if (res.getString(i, "litart").equals("artikel")) mediumType = "Artikel";
				mediaPendingInHTMLTable += "<form name=\"medium" + i + "\" method=\"post\" action=\"index.jsp?content=details&amp;temp_id=" + res.getLong(i, "temp_id") + "\">";
				mediaPendingInHTMLTable += "<tr " + color + ">";
				mediaPendingInHTMLTable += "<td class=\"textright\">" + res.getLong(i, "id") + "</td>";
				mediaPendingInHTMLTable += "<td><input type=\"hidden\" name=\"temp_id\" value=\""+res.getLong(i, "temp_id")+"\" ><input type=\"hidden\" name=\"action\" value=\"details\">" + mediumType + "</td>";
				mediaPendingInHTMLTable += "<td>" + this.showAuthorArray(this.convertToArray(res.getString(i,"author"))) + "</td>";
				mediaPendingInHTMLTable += "<td>" + res.getString(i, "title1") + "</td>";
				mediaPendingInHTMLTable += "<td class=\"textright\">" + res.getString(i, "year") + "</td>";
				mediaPendingInHTMLTable += "<td class=\"textcenter\"><input type=\"submit\" name=\"activate\" value=\"" + this.languageObj.getTextFor("admin.activate") + "\"></input></td>";
				mediaPendingInHTMLTable += "</tr>\n";
				if (files.getRowCount() > 0) {
					for(int j=0; j<files.getRowCount(); j++) {
						mediaPendingInHTMLTable += "<tr " + color + ">";
						mediaPendingInHTMLTable += "<td class=\"textright\">" + files.getLong(j, "id") + "</td>";
						mediaPendingInHTMLTable += "<td><input type=\"checkbox\" name=\"FileStream\" value=\""+files.getInt(j, "id")+"\"></td>";
						mediaPendingInHTMLTable += "<td colspan='3'>" + files.getString(j, "name") + "</td>";
						mediaPendingInHTMLTable += "<td class=\"textcenter\">" + files.getString(j, "MIMETYPE") + "</td>";
						mediaPendingInHTMLTable += "</tr>\n";
					}
				}
						
				mediaPendingInHTMLTable += "</form>";
			}
			mediaPendingInHTMLTable += "</table>";
		}
		
		return mediaPendingInHTMLTable;
	}
	
	/** 
     * Converts author string from database to an array with separate strings for each author
     * @param authorsFromDB String containing authors from database
     * @return Array with seperate strings for each author
     */
	private String[] convertToArray(String authorsFromDB){	
		String regex = "\",\"";
		String [] carved = Pattern.compile(regex).split(authorsFromDB);
		;
		carved[0]=carved[0].substring(1,carved[0].length());
		carved[carved.length-1]=carved[carved.length-1].substring(0,carved[carved.length-1].length()-1);
		return carved;
	}
	
	/** 
     * Generates a string from the given author array
     * @param authors Array with separate strings for each author
     * @return String in readable format
     */
	private String showAuthorArray(String[] authors){
		String myString="";
		for(int i=0;i<authors.length;i++){
			myString+=authors[i]+"<br />";
		}
		myString=myString.substring(0,myString.length()-1);
		return myString;
	}
	
	/** 
     * Set language object to active language object from jsp-file
     * @param languageObj Language object from jsp-file
     */
	public void setLanguage(LanguageBean languageObj) {
		this.languageObj = languageObj;
	}
}
